package com.chaos.eurekaproducer.leetCode;

import com.chaos.eurekaproducer.basic.ListNode;

/**
 * @author liaopeng
 * @title: Offer06
 * @projectName eureka-producer
 * @description:
 * 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）
 * 输入：head = [1,3,2]
 * 输出：[2,3,1]
 * @date 2021/6/7下午8:50
 */
public class Offer06 {
    public static int[] reversePrint(ListNode head){
        int[] a = new int[head.getSize()];
        if (head==null){
            return a;
        }
        ListNode temp = head;
        int i=head.getSize()-1;
        while(temp!=null){
            a[i]=temp.getVal();
            i--;
            temp=temp.getNext();
        }
        return a;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        node4.setNext(node5);
        node3.setNext(node4);
        node2.setNext(node3);
        node1.setNext(node2);
        int[] ints = reversePrint(node1);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }
}
